import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import sun.print.resources.serviceui;

import Entity.Room_user;
import Entity.User;
import tool.HibernateSessionFactory;

public class DAOUser extends DAOAbstract{

	public boolean adduser(User user){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
	}
	

	public Map<String, Object> queryUser(String name){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			Query query = session.createQuery("from User where name = ? and status = 1").setString(0, name);
			List<User> list = query.list();
			Query query2 = session.createQuery("from Room_user where idcard = ? and status = 1").setString(0, list.get(0).getIdcard());
			List<Room_user> list2 = query2.list();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", list.get(0).getName());
			map.put("idcard", list.get(0).getIdcard());
			map.put("roomid", list2.get(0).getRoomid());
			map.put("starttime", list2.get(0).getStarttime());
			session.getTransaction().commit();
			System.out.println(map);
			return map;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	public Map<String, Object> queryUserByIdcard(String idcard){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			Query query = session.createQuery("from User where idcard = ? and status = 1").setString(0, idcard);
			Query query2 = session.createQuery("from Room_user where idcard = ? and status = 1").setString(0, idcard);
			List<User> list = query.list();
			List<Room_user> list2 = query2.list();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", list.get(0).getName());
			map.put("idcard", idcard);
			map.put("roomid", list2.get(0).getRoomid());
			map.put("starttime", list2.get(0).getStarttime());
			session.getTransaction().commit();
			return map;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	public boolean checkOut(String idcard){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			Query query = session.createQuery("from User where idcard = ? and status = 1").setString(0, idcard);
			List<User> list = query.list();
			User user = list.get(0);
			user.setStatus(2);
			session.update(user);
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
	}
}
